function add_industry(){
    var code= jQuery('#BusinessIndustryCode').val();
    var name= jQuery('#BusinessIndustryName').val();
    var href= "/admin/business/business/add_industry/"+code+"/"+name;
    if(code != '' && name !=''){
        jQuery.ajax({
          url: href,
          type: 'post',
          
          success: function(data) {
            
            if(data !=0){
                $('#results').html(data);
                jQuery('#BusinessIndustryCode').val('');
                jQuery('#BusinessIndustryName').val('');
            }else{
                alert('Can not save industry. Please try again!');
            }
           
          }
        });
    }
    else{
        alert("Code and Name can not empty!");
    }
}
function edit_industry(id){
    var code= jQuery('#code_'+id).val();
    var name= jQuery('#name_'+id).val();
    jQuery('#BusinessIndustryCode').val(code);
    jQuery('#BusinessIndustryName').val(name);
    jQuery('#business_industry').attr('onclick', 'save_industry('+id+');');
    jQuery('#business_industry').html("Save Industry");
}
function save_industry(id){
    var code= jQuery('#BusinessIndustryCode').val();
    var name= jQuery('#BusinessIndustryName').val();
    var href= "/admin/business/business/save_industry/"+id+"/"+code+"/"+name;
    if(code != '' && name !=''){
        jQuery.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            if(data !=0){
                $('#results').html(data);
                jQuery('#BusinessIndustryCode').val('');
                jQuery('#BusinessIndustryName').val('');
                jQuery('#business_industry').attr('onclick', 'add_industry();');
                jQuery('#business_industry').html("Add Industry");
            }else{
                alert('Can not save industry. Please try again!');
            }
           
          }
        });
    }
    else{
        alert("Code and Name can not empty!");
    }
}
function delete_industry(id){
    if (confirm("Are you sure you want to delete?")) {
        var href= "/admin/business/business/delete_industry/"+id;
        jQuery.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            if(data !=0){
                $('#results').html(data);
                jQuery('#BusinessIndustryCode').val('');
                jQuery('#BusinessIndustryName').val('');
                jQuery('#business_industry').attr('onclick', 'add_industry();');
                jQuery('#business_industry').html("Add Industry");
            }else{
                alert('Can not delete industry. Please try again!');
            }
           return false;
          }
        });
    }
}
function add_more_file(){
    var element= jQuery("#cloneImage").prev();
    jQuery(element).clone().insertBefore("#cloneImage");
    var $this = Green.Elfinder;
    jQuery('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show(jQuery(this));
            });
}
function delete_attachment(id){
    if (confirm("Are you sure you want to delete?")) {
        var href= "/admin/business/business/delete_attachment/"+id;
        jQuery.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            if(data !=0){
                $('#attachments').html(data);
                
            }else{
                alert('Can not delete attachment. Please try again!');
            }
           return false;
          }
        });
    }
}
function delete_image(id){
    if (confirm("Are you sure you want to delete?")) {
        var href= "/admin/business/business/delete_image/"+id;
        jQuery.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            if(data !=0){
                $('#attachments').html(data);
                
            }else{
                alert('Can not delete attachment. Please try again!');
            }
           return false;
          }
        });
    }
}
function edit_title(id){
   jQuery('#bus_title h1').hide();
   jQuery('#bus_title input').css('display', 'block');
   jQuery('#bus_title_edit').attr('onclick', 'save_title();').html('save');
}
function save_title(){
    var title= jQuery('#bus_title input').val();
    var id = jQuery('#business_id').val();
    var href= "/admin/business/manager/save_title/"+id+"/"+title;
    if(title !=''){
        jQuery.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            if(data !=0){
                $('#bus_title h1').html(title).show();
                jQuery('#bus_title input').css('display', 'none');
                jQuery('#bus_title_edit').attr('onclick', "edit_title("+id+");").html('edit');
            }else{
                alert('Can not save title. Please try again!');
            }
           
          }
        });
    }
    else{
        alert("Title can not empty!");
    }
}
function send_all_business(){
    $('#business_received').html("");
    $('#kcn_business_received').html("");
    $('#ind_business_received').html("");
    $('#business_message input').each(function(){
        $(this).attr("checked", false);
    });
    $('#technology_business input').each(function(){
        $(this).attr("checked", false);
    });
    $('#industries_business input').each(function(){
        $(this).attr("checked", false);
    });
}
function send_business(){
    $('#all_business_received').html("");
    $('#kcn_business_received').html("");
    $('#ind_business_received').html("");
    $('#business_message').dialog("open");
    $('#technology_business input').each(function(){
        $(this).attr("checked", false);
    });
    $('#industries_business input').each(function(){
        $(this).attr("checked", false);
    });
}
function send_kcn_business(){
    $('#all_business_received').html("");
    $('#business_received').html("");
    $('#ind_business_received').html("");
    $('#business_message input').each(function(){
        $(this).attr("checked", false);
    });
    $('#industries_business input').each(function(){
        $(this).attr("checked", false);
    });
    $('#technology_business').dialog("open");
}
function send_industry_business(){
    $('#all_business_received').html("");
    $('#business_received').html("");
    $('#kcn_business_received').html("");
    $('#business_message input').each(function(){
        $(this).attr("checked", false);
    });
    $('#technology_business input').each(function(){
        $(this).attr("checked", false);
    });
    $('#industries_business').dialog('open');
}

jQuery(document).ready(function($){
    $('#checkIndustry').live('click', function(){
       $('input[type=checkbox]').attr('checked', $(this).is(':checked')); 
    });
    var type_of_evestment= $('#BusinessEvestmentTypeOfEvestment').val();
    if(type_of_evestment=='personal'){
        $('#BusinessEvestmentTypeOfEvestment').parent().parent().parent().siblings('.rowset').hide();
    }
    $('#BusinessEvestmentTypeOfEvestment').change(function(){
        var value= $('#BusinessEvestmentTypeOfEvestment').val();
        if(value=='personal'){
            $(this).parent().parent().parent().siblings('.rowset').hide();
        }else{
            $(this).parent().parent().parent().siblings('.rowset').show();
        }
    });
    //add banner for new
    $('#NodeFieldNewImageBanner').parent().parent().parent().hide();
    $('#NodeFieldNewDisplayBanner').click(function(){
       $(this).parent().parent().parent().next('.rowset').toggle(); 
    });
    $('#NodeFieldAboutImageFront').parent().parent().parent().hide();
    $('#NodeFieldAboutDisplayHome').click(function(){
       $(this).parent().parent().parent().next('.rowset').toggle(); 
    });
    //dialog
    $('#business_message').dialog({autoOpen: false, width: 500, modal: true, buttons:{
        Cancel: function() {
			$(this).dialog("close");
		}}});
    $('#technology_business').dialog({autoOpen: false, width: 500, modal: true, buttons:{
        Cancel: function() {
			$(this).dialog("close");
		}}});
    $('#industries_business').dialog({autoOpen: false, width: 500, modal: true, buttons:{
        Cancel: function() {
			$(this).dialog("close");
		}}});
    //check business (type=business)
    $('#business_message input').live('click', function(){
        check= $(this).is(":checked");
        this_value= $(this).val();
        if(check==true){
            //$(this).attr("checked", "checked");
            this_div= $(this).parent().html();
            this_div_parent= "<div>"+this_div+"</div>";
            $('#business_received').append(this_div_parent);
            $('#business_received #bus_'+this_value).attr("id", "bus_append_"+this_value);
            $('#business_received #bus_append_'+this_value).attr("checked", "checked");
            $('#business_received #bus_append_'+this_value).parent().attr("id", "div_bus_append_"+this_value);
        }else{
            $('#business_received #div_bus_append_'+this_value).remove();
        }
    });
    $('#business_received input').live('click', function(){
        this_value= $(this).val();
        $('#business_message #bus_'+this_value).attr("checked", false);
        $(this).parent().remove();
    });
    // bind form using 'ajaxForm' ,
    $('#form-business').live('submit', (function() { 
        var company= jQuery('#BusinessCompanyName').val();
        var industry_id= jQuery('#BusinessIndustryId').val();
        var tech= jQuery('#BusinessTechnology').val();
        var href= "/admin/business/business_message/search/"+company+"/"+industry_id +"/"+tech;
        
        $.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            $('#admin_search_business').html(data);
            if($('#admin_search_business input').length>0){
                $('#business_received input').each(function(){
                    var this_value= $(this).val();
                    var check_box= $('#bus_'+this_value).is(":checked");
                    if(check_box == false){
                        $('#admin_search_business #bus_'+this_value).attr("checked", "checked");
                    }
                });
            }
          }
        });
        return false;
    }));
    //chon khu cong nghiep
    $('#technology_business input').live('click', function(){
        check= $(this).is(":checked");
        this_value= $(this).val();
        if(check==true){
            //$(this).attr("checked", "checked");
            this_div= $(this).parent().html();
            this_div_parent= "<div>"+this_div+"</div>";
            $('#kcn_business_received').append(this_div_parent);
            $('#kcn_business_received #bus_'+this_value).attr("id", "bus_append_"+this_value);
            $('#kcn_business_received #bus_append_'+this_value).attr("checked", "checked");
            $('#kcn_business_received #bus_append_'+this_value).parent().attr("id", "div_bus_append_"+this_value);
        }else{
            $('#kcn_business_received #div_bus_append_'+this_value).remove();
        }
    });
    //huy chon khu cong nghiep
    $('#kcn_business_received input').live('click', function(){
        this_value= $(this).val();
        $('#technology_business #bus_'+this_value).attr("checked", false);
        $(this).parent().remove();
    });
    $('#form-business-industry').live('submit', (function() { 
        var name= jQuery('#BusinessIndustryName').val();
        var href= "/admin/business/business_message/search_industry/"+name;
        $.ajax({
          url: href,
          type: 'post',
          success: function(data) {
            $('#admin_search_industries').html(data);
            if($('#admin_search_industries input').length>0){
                $('#ind_business_received input').each(function(){
                    var this_value= $(this).val();
                    var check_box= $('#bus_'+this_value).is(":checked");
                    if(check_box == false){
                        $('#admin_search_industries #bus_'+this_value).attr("checked", "checked");
                    }
                });
            }
          }
        });
        return false;
    }));
    //chon nganh nghe
    $('#industries_business input').live('click', function(){
        check= $(this).is(":checked");
        this_value= $(this).val();
        if(check==true){
            //$(this).attr("checked", "checked");
            this_div= $(this).parent().html();
            this_div_parent= "<div>"+this_div+"</div>";
            $('#ind_business_received').append(this_div_parent);
            $('#ind_business_received #bus_'+this_value).attr("id", "bus_append_"+this_value);
            $('#ind_business_received #bus_append_'+this_value).attr("checked", "checked");
            $('#ind_business_received #bus_append_'+this_value).parent().attr("id", "div_bus_append_"+this_value);
        }else{
            $('#ind_business_received #div_bus_append_'+this_value).remove();
        }
    });
    //huy chon nganh nghe
    $('#ind_business_received input').live('click', function(){
        this_value= $(this).val();
        $('#industries_business #bus_'+this_value).attr("checked", false);
        $(this).parent().remove();
    });
    //chon khu cong nghiep
    $('#FilterTechnologyId').change(function(){
       var technology= $('#FilterTechnologyId').val();
       if(technology==1){
            var tech_option= $('#technology').html();
            $('#FilterItemId').html(tech_option);
       }else if(technology==2){
            var industries= $('#industries').html();
            $('#FilterItemId').html(industries);
       }else{
            $('#FilterItemId').html("<option value=''>-- Any --</option>");
       }
    });
});
